package com.atguigu.day04;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

//阻塞队列练习
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        //没有无参构造方法，必须指定初始值
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        BlockingQueue blockingQueue2 = new ArrayBlockingQueue(3);
        BlockingQueue blockingQueue3 = new ArrayBlockingQueue(3);
        BlockingQueue blockingQueue4 = new ArrayBlockingQueue(3);
        //第一组   抛出异常  add  remove  element
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.add("d")); //报错，只有三个位置
        System.out.println(blockingQueue.element());//查找队首元素
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());//报错
        System.out.println("===========================================");
        //第二组  特殊值  offer  poll  peek
        System.out.println(blockingQueue2.offer("a"));
        System.out.println(blockingQueue2.offer("b"));
        System.out.println(blockingQueue2.offer("c"));
        System.out.println(blockingQueue2.offer("d"));//false

        System.out.println(blockingQueue2.peek());//查找队首元素

        System.out.println(blockingQueue2.poll());
        System.out.println(blockingQueue2.poll());
        System.out.println(blockingQueue2.poll());
        System.out.println(blockingQueue2.poll());//null,因为只有三个元素

        System.out.println("===========================================");
        //第三组 阻塞  put  take
        blockingQueue3.put("a");
        blockingQueue3.put("b");
        blockingQueue3.put("c");
        System.out.println("111111");
        //会打印出111111，但不会打印出222222，因为d没有被添加进去，这时候既不报错也不false，程序未结束，就一直阻塞着
//        blockingQueue3.put("d");//put没有返回值
        System.out.println("222222");
        //移除元素
        System.out.println(blockingQueue3.take());
        System.out.println(blockingQueue3.take());
        System.out.println(blockingQueue3.take());
//        System.out.println(blockingQueue3.take());//无法移除,因为已经没有元素了,所以一直阻塞

        //第四组 超时  offer(e,time,unit)   poll(time,unit)

        System.out.println("===============================");
        System.out.println(blockingQueue4.offer("a",2,TimeUnit.SECONDS));//返回值为boolean
        System.out.println(blockingQueue4.offer("b",2,TimeUnit.SECONDS));
        System.out.println(blockingQueue4.offer("c",2,TimeUnit.SECONDS));
        System.out.println(blockingQueue4.offer("d",2,TimeUnit.SECONDS));
        //当队列满的时候，会等待2秒钟，超过两秒该生产者线程就会退出
        System.out.println(blockingQueue4.poll());
        System.out.println(blockingQueue4.poll());
        System.out.println(blockingQueue4.poll());
        System.out.println(blockingQueue4.poll());//显示为null，无法移除

    }
}
